package com.fengba.nanzhi.tabbar01;

import android.content.Context;
import android.content.Intent;
import android.content.SyncAdapterType;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.fengba.nanzhi.fengba.R;
import com.fengba.nanzhi.login.Login;
import com.fengba.nanzhi.myfengba.MyCF;
import com.fengba.nanzhi.notice.Notice;
import com.fengba.nanzhi.utils.FX_SignInUtil;
import com.fengba.nanzhi.utils.Function;
import com.fengba.nanzhi.utils.Preferences;
import com.fengba.nanzhi.utils.ToastUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class HeaderNumberedAdapter extends RecyclerView.Adapter<TextViewHolder> {
    private static final int ITEM_VIEW_TYPE_HEADER = 0;
    private static final int ITEM_VIEW_TYPE_ITEM = 1;

    private final View header;
    private final List<String> labels;

    private JSONArray jsonArray;
    private Tabbar01 notice;
    private JSONObject job;
    private Context context;
    private int widths;
    private TextViewHolder tvh;

    public HeaderNumberedAdapter(View header, int count, Context context, JSONArray jsonData) {
        if (header == null) {
            throw new IllegalArgumentException("header may not be null");
        }
        this.header = header;
        this.labels = new ArrayList<String>(count);
        for (int i = 0; i < count; ++i) {
            labels.add(String.valueOf(i));
        }
        this.jsonArray = jsonData;
        this.context = context;
    }

    public boolean isHeader(int position) {
        return position == 0;
    }

    @Override
    public TextViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //tvh = new TextViewHolder(view);
        if (viewType == ITEM_VIEW_TYPE_HEADER) {
            return new TextViewHolder(header);
        }
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fx_psq_view, parent, false);
        return new TextViewHolder(view);
    }

    @Override
    public void onBindViewHolder(TextViewHolder holder, final int position) {
        if (isHeader(position)) {
            return;
        }
        try {
            job = (JSONObject) jsonArray.get(position-1);
            widths = Function.getScreenWidth(context);
            holder.Piv01.getLayoutParams().width = widths / 2;
            holder.Piv01.getLayoutParams().height = widths / 2;
            holder.textView.getLayoutParams().width = widths / 2;
            holder.textView.setText(job.get("name").toString());
            if (context != null) {
                Glide.with(context).load(job.get("icon").toString()).into(holder.imageView);
            }
            holder.FPSQ01.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (FX_SignInUtil.switchAccount(context)) {
                        Toast.makeText(context,"请前往登录",Toast.LENGTH_SHORT).show();
                    }
                    String id = null;
                    try {
                        Object user_info = Preferences.getParam(context, "user_info", "");
                        JSONObject myJson = null;
                        myJson = new JSONObject(user_info.toString());
                        id = myJson.get("id").toString();
                        Intent intent = new Intent(context, FX_psq_game.class);
                        intent.putExtra("id", id);
                        intent.putExtra("type", ((JSONObject) jsonArray.get(position-1)).get("type").toString());
                        intent.putExtra("crazyrob_id", ((JSONObject) jsonArray.get(position-1)).get("id").toString());
                        context.startActivity(intent);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
        /*if (notice != null) {
            try {
                job = (JSONObject) jsonArray.get(position);
                //System.out.println(job.toString());
                widths = notice.getwidth();
                holder.Piv01.getLayoutParams().width = widths / 2;
                holder.Piv01.getLayoutParams().height = widths / 2;
                holder.textView.setText(job.get("name").toString());
                if (context != null) {
                    Glide.with(context).load(job.get("icon").toString()).into(holder.imageView);
                }
                tvh.FPSQ01.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String id = null;
                        try {
                            Object user_info = Preferences.getParam(context, "user_info", "");
                            JSONObject myJson = null;
                            myJson = new JSONObject(user_info.toString());
                            id = myJson.get("id").toString();
                            Intent intent = new Intent(context, FX_psq_game.class);
                            intent.putExtra("id", id);
                            intent.putExtra("type", ((JSONObject) jsonArray.get(position)).get("type").toString());
                            intent.putExtra("crazyrob_id", ((JSONObject) jsonArray.get(position)).get("id").toString());
                            context.startActivity(intent);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }*/
    }

    @Override
    public int getItemViewType(int position) {
        return isHeader(position) ? ITEM_VIEW_TYPE_HEADER : ITEM_VIEW_TYPE_ITEM;
    }

    @Override
    public int getItemCount() {
        ////Log.e("222",jsonArray.toString());
        if (jsonArray != null) {
            return jsonArray.length()+1;
        } else {
            return 1;
        }
    }
}
